<?php
# file will contain core functions for meeting description page.

# function that will return list of meeting participants with their ready status.
function c_get_meet_data($meet_id){
	
	$meeting=array();
	
	# get meeting data
	$query = "SELECT PLACE, MIN_TIME, MAX_TIME, GROUP_ID, DURATION FROM prop_meeting WHERE ID = $meet_id";
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$meeting['place'] = $row[0];
			$meeting['min_time'] = $row[1];
			$meeting['max_time'] = $row[2];
			$group_id = $row[3];
			$meeting['group_id'] = $group_id;
			$meeting['duration'] = $row[4];
		}
	}
	
	#get group name
	$query = "SELECT NAME FROM groups WHERE ID = $group_id";
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$meeting['group_name'] = $row[0];
		}
	}

	# get list of group participants
	$query = "SELECT u.ID, u.NAME FROM participants p JOIN user u ON p.USER_ID = u.ID
					WHERE p.GROUP_ID = $group_id ORDER BY NAME ASC";
	$result = c_db_query($query);
	
	$participants = array();
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			if($row[0] != $_SESSION['user_id']){ // add participant to list only if he is not group leader
				$participants[] = array('id' => $row[0], 'name' => $row[1], 'status' => 0); // by default - not ready
			}
		}
	}
	
	# get list of ready participants
	$query = "SELECT USER_ID FROM prop_participants WHERE MEET_ID = $meet_id";
	$result = c_db_query($query);
	
	$ready_list = array();
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$ready_list[] = $row[0];
		}
	}
	
	#mark all ready participants as ready
	$ci = sizeof($participants);
	for($i = 0;$i<$ci;$i++){
		if(in_array($participants[$i]['id'], $ready_list)) $participants[$i]['status'] = 1;
	}
	$meeting['participants'] = $participants;
	return $meeting;
}

# function that will suggest best time for current situation
function suggest_time($meeting){
	$group_id = $meeting['group_id'];
	
	$min_date = date('Y-m-d', strtotime($meeting['min_time']));
	$max_date = date('Y-m-d', strtotime($meeting['max_time']));
	
	
	# get list of group participants (including master)
	$query = "SELECT u.ID, u.NAME FROM participants p JOIN user u ON p.USER_ID = u.ID
					WHERE p.GROUP_ID = $group_id ORDER BY NAME ASC";
	$result = c_db_query($query);
	
	$participants = array();
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$participants[] = array('id' => $row[0], 'name' => $row[1]);
		}
	}
	
	#get available times of each group participant
	$ci = sizeof($participants);
	for($i=0;$i<$ci;$i++){
		$part_id = $participants[$i]['id'];
	
		$query = "SELECT DATE, HOUR FROM timetable 
					WHERE USER_ID = $part_id 
							AND DATE >= CAST('$min_date' AS DATE) 
							AND DATE <= CAST('$max_date' AS DATE)";
		
		$result = c_db_query($query);
		if($result){
			$participants[$i]['times'] = array();
			while($row = mysql_fetch_array($result)) 
			{
				$participants[$i]['times'][] = array('date' => $row[0], 'time' => $row[1]);
			}
		}
	}
	
	# do calculation
	$cur_time = strtotime($meeting['min_time']); // tracks current time i'm checking
	$max_time = strtotime($meeting['max_time']); // max time, after which it is not allowed to look
	$duration = $meeting['duration']; // number of hours that must be free
	// change max time so that maximal allowed time will be 1 second more than "max time" - 1 hour.
	$max_time = $max_time - ($duration*3600) + 1;
	
	$best_time = $cur_time; // will hold best time found
	$best_count = 0; // will hold best amount found
	
	$c_part = sizeof($participants); // holds number of participants in group
	
	while($cur_time < $max_time && $best_count < $c_part){ // while we still can look and best possible time not found
		
		$ready_count = 0; // will count how many participants are available in current time
		
		$cj = $c_part; // number of participants in group
		for($j=0;$j<$cj;$j++){ // for each group participant check his availability in selected time
			
			$test_time = $cur_time; // time that will be used to test each hour of duration
			$test_count = 0; // hold number of hours of duration, that checked person will be available (of full duration)
			
			$ci = $duration;
			for($i=0;$i<$ci;$i++){ // check availability for full duration (1 loop cycle for 1 hour of duration)
				
				$test_date = date('Y-m-d', $test_time); // correct date format for tested hour
				$test_hour = intval(date('H', $test_time)); // correct hour format for test date
				$test_array = array('date' => $test_date, 'time' => $test_hour); // correct foramt array that will represent availability in this time
				
				if(in_array($test_array, $participants[$j]['times'])){ // if test array is found in list of participant arrays, in this hour he is available
					$test_count++; // increase number of hours that user will be available
				}
				$test_time = strtotime("+1 hour", $test_time); // go to next hour
			}
			
			if( $test_count == $duration ) $ready_count++; // if participant is available for full duration, he is available in current time
		}
		
		if( $ready_count > $best_count ){ // if more participants are available in current time, than previous best, shift best time to this.
			$best_count = $ready_count; // new best number of available participants
			$best_time = $cur_time; // time with this much participants
		}
		
		$cur_time = strtotime("+1 hour",$cur_time);
	}
	
	$best_date = date('Y-m-d H:i:s', $best_time);
	
	$result = array('date' => $best_date, 'count' => $best_count);
	return $result;
}
?>